import {
    createRouter,
    createWebHistory
} from 'vue-router'

const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [{
        path: '/',
        name: 'Login',
        component: () => import('../views/user_login/Login.vue')
    },
        {
            path: '/about',
            name: 'about',
            component: () => import('../views/AboutView.vue')
        }
        ,
        {
            path: '/master',
            name: 'master',
            component: () => import('../views/master/master.vue'),
            children: [
                {
                    path: '/user_data',
                    name: 'user_data',
                    component: () => import('../views/data/user_data.vue')
                },
                {
                    path: '/aprv_data',
                    name: 'aprv_data',
                    component: () => import('../views/data/aprv_data.vue')
                }
            ]
        }
    ]
})

import {isAuthenticated} from '../utils/auth.js'
import {notification} from "ant-design-vue";
import {h} from "vue";
import {SmileOutlined} from "@ant-design/icons-vue";

router.beforeEach(async (to, from, next) => {
    if (
        // 检查用户是否已登录
        !isAuthenticated() &&
        // ❗️ 避免无限重定向
        to.name !== 'Login'
    ) {
        notification.open({
            message: '登陆失效',
            description: '当前登陆失效，请重新登陆！！',
            icon: () => h(SmileOutlined, {style: 'color: #108ee9'})
        });
        // 将用户重定向到登录页面
        return next({
            path: 'Login'
        });
    }
    next();
});

export default router